<?php
namespace Home\Controller;
use Home\Controller\EmptyController;
class CommonController extends EmptyController {
   
	public function _initialize(){
        if(empty($_SESSION[C('USER_AUTO_KEY')])){
            $this->redirect('Public/login');
        }
	}

	/**
     +----------------------------------------------------------
     * Index页显示
     *
     */
    public function index( $db_name = '' )
    {
        
        //列表过滤器，生成查询Map对象
        $db_name = $db_name ? $db_name : CONTROLLER_NAME;
        $model   = D( $db_name );
        $map     = $this->_search( $db_name );

        // dump($map);
        $this->assign( "map", $map );
        if( method_exists( $this, '_filter' ) )
        {
            $this->_filter( $map );
        }
        if( !empty( $model ) )
        {
            $this->_list( $model, $map );
        }
        
        $this->display();
    }


    /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected
     +----------------------------------------------------------
     * @param string $db_name 数据对象名称
     +----------------------------------------------------------
     * @return HashMap
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _search( $db_name = '' )
    {
        $db_name = $db_name ? $db_name : CONTROLLER_NAME;
        
        // dump($db_name);
        //生成查询条件
        $model   = D( $db_name );
        $map     = array();
        foreach( $model->getDbFields() as $key     => $val )
        {
            if( isset( $_REQUEST[ $val ] ) && $_REQUEST[ $val ] != '' )
            {
                $map[ $val ]         = $_REQUEST[ $val ];
            }
        }
        return $map;
    }
    

      /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected
     +----------------------------------------------------------
     * @param Model $model 数据对象
     * @param HashMap $map 过滤条件
     * @param string $sortBy 排序
     * @param boolean $asc 是否正序
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _list( $model, $map, $sortBy   = '', $asc      = false )
    {
        
        //排序字段 默认为主键名
        if( !empty( $_REQUEST['_order'] ) )
        {
            $order    = $_REQUEST['_order'];
        } 
        else
        {
            $order    = !empty( $sortBy ) ? $sortBy : $model->getPk();
        }
        
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if( isset( $_REQUEST['_sort'] ) )
        {
            $sort     = $_REQUEST['_sort'] == 'asc' ? 'asc' : 'desc';
        } 
        else
        {
            $sort     = $asc ? 'asc' : 'desc';
        }

        //取得满足条件的记录数
        $count = $model->where( $map )->count();

        if( $count > 0 )
        {
            
            //创建分页对象  ,默认10条记录
            if( !empty( $_REQUEST['listRows'] ) )
            {
                $listRows = $_REQUEST['listRows'];
            } 
            else
            {
                $listRows = 10;
            }


            $p  = new \Think\Page( $count, $listRows,$search);
  
            //分页查询数据
            $voList   = $model
                ->where( $map )
                ->order( $order . ' ' . $sort )
                ->limit( $listRows )
                ->page( $_REQUEST[C( 'VAR_PAGE' )] )->select();

            //分页显示
            $page    = $p->show();
            
            //排序提示
            $sort    = $sort == 'desc' ? 1 : 0;
            //排序方式
            //模板赋值显示
            $this->assign( 'list', $voList );
            $this->assign( 'page', $page );

        }
        
        return;
    }
    

    public function add(){

        if(IS_POST || IS_AJAX){
            $db_name = CONTROLLER_NAME;
            $model   = D( $db_name );
            if( false === $model
                ->create() )
            {
                $this->error( $model->getError() );
            }
            
            //保存当前数据对象
            $list = $model->add();
            if( $list !== false )
            {
                $this->success(L('_OPERATION_SUCCESS_'));
            } 
            else
            {
                $this->error(L('_OPERATION_FAIL_'));
            }
        }else{
            $this->display();
        }
    }



    public function edit(){

        $db_name = CONTROLLER_NAME;
        if(IS_POST || IS_AJAX){
            $model   = D( $db_name );
            $data = $model->create();
            if( false === $model->create() )
            {
                $this->error( $model->getError() );
            }
            
            // 更新数据
            $list = $model->save();
           
            if( false !== $list )
            {
                
                //成功提示
                $this->success( '编辑成功!');
            } 
            else
            {
                //错误提示
                $this->error( '编辑失败!' );
            }
        }else{
            $model   = M( $db_name );
            $id      = I( $model->getPk() );
            $vo      = $model->find( $id );
            $this->assign( 'vo', $vo );
            $this->display();
        }
    }


    public function foreverdelete()
    {
        $db_name   = CONTROLLER_NAME;
        //删除指定记录
        $model     = D( $db_name );
        if( !empty( $model ) )
        {
            $pk        = $model->getPk();
            $id        = $_REQUEST[ $pk ];
            if( isset( $id ) )
            {
                $condition = array(
                    $pk => array(
                        'in',
                        explode( ',', $id )
                    )
                );
                if( false !== $model
                    ->where( $condition )
                    ->delete() )
                {
                    $this->success(L('_OPERATION_SUCCESS_'));
                } 
                else
                {
                    $this->error(L('_OPERATION_FAIL_'));
                }
            } 
            else
            {
                $this->error(L('_ERROR_ACTION_'));
            }
        }else{
            $this->error(L('_ERROR_ACTION_'));
        }
    }



}